package com.jeecg.xiaozhi.qa.controller;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jeecgframework.core.util.AuthService;
import org.jeecgframework.core.util.GsonUtils;
import org.jeecgframework.core.util.HttpUtil;
import org.json.JSONArray;
import org.json.JSONObject;  
public class RecComment_TagClss {
	 // #####调用鉴权接口获取的token#####  
    public String access_token = "luozhuang";  
    int getaccesstokentime;  
    //String comment_tag_Url = "https://aip.baidubce.com/rpc/2.0/nlp/v2/comment_tag"; 
    //词法分析
    String comment_tag_Url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer";  
  
    public String getaccess_token() {  
        AuthService temp = new AuthService();  
        access_token = AuthService.getAuth();  
        return access_token;  
    }  
  
    public Map<String,String> comment_tag(String accessToken, String comment) {  
        try {  
            comment = URLDecoder.decode(comment, "utf-8");  
        } catch (UnsupportedEncodingException e1) {  
  
            e1.printStackTrace();  
        }  
        String result = null;  
        Map<String,String> maprep=new HashMap<String, String>();  
        // 评论观点抽取url  
        if (comment == null) {  
            return maprep;  
        }  
  
        try {  
            Map<String, Object> map = new HashMap<String, Object>();  
            // comment = URLEncoder.encode(comment, "GBK");  
            map.put("text", comment); // 评论内容  
            //map.put("type", 13); // 类别  ：只对评论接口有效
            String params = GsonUtils.toJson(map);  
  
            /** 
             * 线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。 
             */  
  
            result = HttpUtil.post(comment_tag_Url, accessToken, params);  
  
            System.out.println(result);  
            JSONObject jsonObject = new JSONObject(result);  
            if (jsonObject.has("error_code") == true) {  
                int error=jsonObject.getInt("error_code");  
                if("282130".equals(String.valueOf(error)))  
                {   
                return maprep;//"282130 没有识别到数据";  
                }  
                else  
                {  
                return maprep;//"错误:" + jsonObject.getString("error_msg");  
                }  
            }  
            if (jsonObject.has("items") == true) {  
  
                JSONArray item = jsonObject.getJSONArray("items");  
                result = "";  
                if (item == null) {  
                    return maprep;  
                }  
                List<String> ls_pos=new ArrayList<String>();
                String str_pos="";
                List<String> ls_item=new ArrayList<String>();
                for (int i = 0; i < item.length(); i++) {  
                    jsonObject = item.getJSONObject(i);  
//                    result = result + ";" + jsonObject.getString("abstract");  
                    ls_pos.add(jsonObject.getString("pos"));
                    str_pos=str_pos+jsonObject.getString("pos");
                    ls_item.add(jsonObject.getString("item"));
                }  
                
                //语法构成
                /**
                 v+r+n，普通动词+代词+普通名词，感冒吃什么药
                 */
                if(str_pos.contains("vrn")) {
                	//普通动词+代词+普通名词
                	int n=str_pos.indexOf("vrn")+2;
                	maprep.put("resp", ls_item.get(n));
                }
                
                
            }  
  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return maprep;  
    }  
  
    public static void main(String args[]) {  
        RecComment_TagClss tempTagClss = new RecComment_TagClss();  
        /* 
         * AuthService temp=new AuthService(); String 
         * access_token=temp.getAuth(); 
         */  
        String comment = "感冒吃什么药";  
        tempTagClss.comment_tag(tempTagClss.getaccess_token(), comment);  
    }  
}
